package com.example.testapp.activity.activity.menu;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.Toast;

import com.example.testapp.R;
import com.example.testapp.activity.util.LunarCalendar;
import com.example.testapp.activity.view.BorderTextView;

/**
 * Created by l on 2016/6/11.
 */
public class CalendarConvertActivity extends Activity {

    private LunarCalendar lc = null;
    private Toolbar   convertBar = null;
    private BorderTextView convertDate = null;
    private BorderTextView convertBT = null;
    private TextView  lunarDate = null;

    private int year_c;
    private int month_c;
    private int day_c;

    public CalendarConvertActivity(){
        lc = new LunarCalendar();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_convert);

        initView();
    }

    private void initView() {
        convertBar = (Toolbar)findViewById(R.id.convert_toolbar);
        convertBar.setTitle("日期转换");
        convertBar.setTitleTextColor(Color.WHITE);
        convertDate = (BorderTextView) findViewById(R.id.convertDate);
        convertBT = (BorderTextView) findViewById(R.id.convert);
        lunarDate = (TextView) findViewById(R.id.convertResult);

        Intent intent = getIntent();
        final int[] date = intent.getIntArrayExtra("date");
        year_c = date[0];
        month_c = date[1];
        day_c = date[2];
        convertDate.setText(year_c+"年"+month_c+"月"+day_c);

        convertDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               new DatePickerDialog(CalendarConvertActivity.this, new DatePickerDialog.OnDateSetListener() {
                   @Override
                   public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                       if(year < 1901 || year > 2049){
                           Toast.makeText(CalendarConvertActivity.this, "跳转日期范围1900/1/1-2049/12/31", Toast.LENGTH_SHORT).show();
                       }else{
                           year_c = year;
                           month_c = monthOfYear + 1;
                           day_c = dayOfMonth;
                           convertDate.setText(year_c+"年"+month_c+"月"+day_c);
                       }
                   }
               }, year_c, month_c-1, day_c).show();
            }
        });
        convertBT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String lunarDay = getLunarDay(year_c, month_c, day_c);
                String lunarYear = String.valueOf(lc.getYear());
                String lunarMonth = lc.getLunarMonth();

                lunarDate.setText(lunarYear+"年"+lunarMonth+lunarDay);
            }
        });
    }

    private String getLunarDay(int year, int month, int day) {
        String  lunarDay = lc.getLunarDate(year, month, day, true);
        if(lunarDay.substring(1, 2).equals("月")){
            lunarDay = "初一";
        }
        return lunarDay;
    }
}
